﻿Imports System.Xml.Serialization
Imports System.IO

Public Class FormMapEditor
	Public Map As Map
	Public Timer As DisplayTimer
	Public rand As New Random()

	Public MapSelectionTiles(,) As MapTile
	Public MapSelectionTileList As New List(Of MapTile)
	Public MapSelectionTileTable As New Hashtable()
	Public TileSelectionWidth As Integer = 27
	Public TileSelectionHeihgt As Integer = 4
	Public SelectedTile As MapTile

#Region "Initialize"
	Public Sub New()

		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.
		Map = New Map(25, 16)

		ConfigureTiles()

		Timer = New DisplayTimer(AddressOf UpdateMap, 50)
		Timer.StartTask()
	End Sub

	'Add all the tiles here, {filename, an unique ID, T/F on whether its solid}
	Public Sub AddTiles()
		'Ground Tiles
		AddTile("BMP\Tiles\Outside\Grass.bmp", "Grass", False)

		'Roads Tiles
		AddTile("BMP\Tiles\Outside\Road1.bmp", "Road1", False)
		AddTile("BMP\Tiles\Outside\Road2.bmp", "Road2", False)
		AddTile("BMP\Tiles\Outside\Road3.bmp", "Road3", False)
		AddTile("BMP\Tiles\Outside\RoadCorner1.bmp", "RoadCorner1", False)
		AddTile("BMP\Tiles\Outside\RoadCorner2.bmp", "RoadCorner2", False)
		AddTile("BMP\Tiles\Outside\RoadCorner3.bmp", "RoadCorner3", False)
		AddTile("BMP\Tiles\Outside\RoadCorner4.bmp", "RoadCorner4", False)
		AddTile("BMP\Tiles\Outside\RoadT1.Bmp", "RoadT1", False)
		AddTile("BMP\Tiles\Outside\RoadT2.Bmp", "RoadT2", False)
		AddTile("BMP\Tiles\Outside\RoadT3.Bmp", "RoadT3", False)
		AddTile("BMP\Tiles\Outside\RoadT4.Bmp", "RoadT4", False)

		'Outside Object Tiles
		AddTile("BMP\Tiles\Outside\Tree.bmp", "Tree", True)
		AddTile("BMP\Tiles\Outside\Rock.bmp", "Rock", True)

		AddTile("BMP\Tiles\Outside\A_Grass_map1.bmp", "GrassMap1", False, 9, 9)


	End Sub

	Public Sub ConfigureTiles()
		ReDim MapSelectionTiles(TileSelectionWidth, TileSelectionHeihgt)

		Try
			AddTiles()

			SetActiveMapTile(MapSelectionTileList(0))

			For x = 0 To Map.Width - 1
				For y = 0 To Map.Height - 1
					PlaceTile(SelectedTile, x, y)
				Next
			Next

			Dim TileSelectionImage As New Bitmap(TileSelectionsImage.Width, TileSelectionsImage.Height)
			Dim TileSelectionGraphics As Graphics = Graphics.FromImage(TileSelectionImage)

			Dim count As Integer = 0
			For y = 0 To TileSelectionHeihgt - 1
				For x = 0 To TileSelectionWidth - 1
					If count >= MapSelectionTileList.Count Then
						Exit For
					End If
					MapSelectionTiles(x, y) = MapSelectionTileList(count)
					TileSelectionGraphics.DrawImage(MapSelectionTileList(count).BitMap, x * (MapTile.Width + 1), y * (MapTile.Height + 1), MapTile.Width, MapTile.Height)
					count += 1
				Next
				If count >= MapSelectionTileList.Count Then
					Exit For
				End If
			Next
			TileSelectionsImage.Image = TileSelectionImage
		Catch ex As Exception
			Stop
		End Try

	End Sub

	Public Sub AddTile(FileName As String, tileid As String, solid As Boolean)
		Try
			Dim stream As New IO.FileStream(FileName, IO.FileMode.Open)
			Dim maptile As New MapTile(New Bitmap(stream), tileid, solid)
			MapSelectionTileList.Add(maptile)
			MapSelectionTileTable(tileid) = maptile
			stream.Close()
		Catch ex As Exception
			Stop
		End Try
	End Sub

	Public Sub AddTile(FileName As String, tileid As String, solid As Boolean, width As Integer, height As Integer)
		Try
			Dim stream As New IO.FileStream(FileName, IO.FileMode.Open)
			Dim bitmap As New Bitmap(stream)
			stream.Close()
			'Dim bitmap As Bitmap = New Bitmap(stream) 'same as other declaration
			Dim counter As Integer = 0
			Dim scaledbitmap As New Bitmap(width * MapTile.Width, height * MapTile.Height)
			Dim ScaleGraphics As Graphics = Graphics.FromImage(scaledbitmap)
			ScaleGraphics.DrawImage(bitmap, 0, 0, width * MapTile.Width, height * MapTile.Height)
			For x = 0 To width - 1
				For y = 0 To height - 1
					Dim newBitmap As New Bitmap(MapTile.Width, MapTile.Height)
					Dim graphics As Graphics = graphics.FromImage(newBitmap)
					graphics.DrawImage(scaledbitmap, -x * MapTile.Width, -y * MapTile.Height)
					graphics.Dispose()

					Dim newTileID As String = tileid + "_" + counter.ToString()
					Dim maptile2 As New MapTile(newBitmap, newTileID, solid)
					MapSelectionTileList.Add(maptile2)
					MapSelectionTileTable(newTileID) = maptile2
					counter += 1
				Next
			Next


		Catch ex As Exception
			Stop
		End Try
	End Sub
#End Region


#Region "Main Map"
	Public Delegate Sub UpdateMapDelegate()
	Public Sub UpdateMap()
		If Me.InvokeRequired Then
			Me.Invoke(New UpdateMapDelegate(AddressOf UpdateMap))
		Else
			Map.Update()
			MainMapImage.Image = Map.BitMap
			Update()
		End If
	End Sub

	Private Sub MainMap_Click(sender As Object, e As EventArgs) Handles MainMapImage.Click
		Dim mouse As MouseEventArgs = e
		If mouse.Button = Windows.Forms.MouseButtons.Left Then
			PlaceSelectedTile()
		ElseIf mouse.Button = Windows.Forms.MouseButtons.Right Then
			Dim point As Point = MainMapImage.PointToClient(MousePosition)
			Dim x As Double = point.X
			Dim y As Double = point.Y
			x = Math.Round(x / MapTile.Width - 0.5)
			y = Math.Round(y / MapTile.Height - 0.5)
			If x >= 0 And y >= 0 And x < Map.Width And y < Map.Height Then
				Map.MapTileMatrix(x)(y).Rotation = (Map.MapTileMatrix(x)(y).Rotation + 90) Mod 360
				Map.MapTileMatrix(x)(y).Rotate()
			End If
		End If

	End Sub

	Private Sub MainMap_MouseMove(sender As Object, e As MouseEventArgs) Handles MainMapImage.MouseMove
		If e.Button = Windows.Forms.MouseButtons.Left Then
			PlaceSelectedTile()
		End If
	End Sub

	Private Sub PlaceSelectedTile()
		Dim point As Point = MainMapImage.PointToClient(MousePosition)
		Dim x As Double = point.X
		Dim y As Double = point.Y
		x = Math.Round(x / MapTile.Width - 0.5)
		y = Math.Round(y / MapTile.Height - 0.5)

		PlaceTile(SelectedTile, x, y)
	End Sub

	Private Sub PlaceTile(tile As MapTile, x As Integer, y As Integer)
		If x >= 0 And y >= 0 And x < Map.Width And y < Map.Height Then
			Map.MapTileMatrix(x)(y).CopyFrom(tile)
		End If
	End Sub
#End Region

#Region "Tile Selection"
	Private Sub TileSelections_Click(sender As Object, e As EventArgs) Handles TileSelectionsImage.Click
		Dim point As Point = TileSelectionsImage.PointToClient(MousePosition)
		Dim x As Double = point.X
		Dim y As Double = point.Y
		x = Math.Round(x / (MapTile.Width + 1) - 0.5)
		y = Math.Round(y / (MapTile.Height + 1) - 0.5)

		If x >= 0 And y >= 0 And x < TileSelectionWidth And y < TileSelectionHeihgt Then
			SetActiveMapTile(MapSelectionTiles(x, y))
		End If
	End Sub

	Public Sub SetActiveMapTile(maptile As MapTile)
		If maptile IsNot Nothing Then
			SelectedTile = maptile
			Dim bitmap As New Bitmap(100, 100)
			Dim graphics As Graphics = Drawing.Graphics.FromImage(bitmap)
			graphics.DrawImage(SelectedTile.BitMap, 0, 0, 100, 100)
			SelectedTileImage.Image = bitmap
			graphics.Dispose()
		End If
	End Sub
#End Region

#Region "Form"
	Private Sub FormMapEditor_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
		Timer.StopTask()
		Dim n As Integer = 0
		For n = 1 To 100
			Application.DoEvents()
			If Timer.task.Wait(100) Then
				Exit For
			End If
		Next
	End Sub
#End Region

#Region "Menu"
	Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
		SelectedTile = MapSelectionTileList(0)
		For x = 0 To Map.Width - 1
			For y = 0 To Map.Height - 1
				PlaceTile(SelectedTile, x, y)
			Next
		Next
	End Sub

	Private Sub LoadToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadToolStripMenuItem.Click
		Dim dialog As New OpenFileDialog()
		dialog.Filter = "map files (*.map)|*.map|All files (*.*)|*.*"
		dialog.RestoreDirectory = True
		Dim result = dialog.ShowDialog()

		If result = Windows.Forms.DialogResult.OK Then
			Try
				Dim serializer As New XmlSerializer(GetType(Map))
				Dim fs As New FileStream(dialog.FileName, FileMode.Open)
				Map = CType(serializer.Deserialize(fs), Map)
				fs.Close()
				Map.FinishLoad()
				For x As Integer = 0 To Map.Width - 1
					For y As Integer = 0 To Map.Height - 1
						Map.MapTileMatrix(x)(y).CopyFrom(MapSelectionTileTable(Map.MapTileMatrix(x)(y).TileID))
					Next
				Next
			Catch ex As Exception

			End Try

		End If

	End Sub

	Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
		Dim dialog As New SaveFileDialog()
		dialog.Filter = "map files (*.map)|*.map|All files (*.*)|*.*"
		dialog.RestoreDirectory = True
		Dim result = dialog.ShowDialog()

		If result = Windows.Forms.DialogResult.OK Then
			Try
				Dim serializer As New XmlSerializer(GetType(Map))
				Dim writer As New StreamWriter(dialog.FileName)
				serializer.Serialize(writer, Map)
				writer.Close()
			Catch ex As Exception

			End Try

		End If

	End Sub
#End Region
End Class